<template>
  <div class="bg">
    <div class="title">简易聊天室</div>
    <div class="form">
      <el-form
        :model="form"
        label-width="auto"
        :rules="rules"
        class="elForm"
        ref="elFormRef">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" />
        </el-form-item>
        <el-form-item label="密码" prop="userPass">
          <el-input
            v-model="form.userPass"
            type="password"
            :show-password="true" />
        </el-form-item>
        <el-form-item label="年龄" prop="userAge">
          <el-input-number
            v-model="form.userAge"
            :min="1"
            :max="200"
            style="width: 100%" />
        </el-form-item>
        <el-form-item label="性别" prop="userSex">
          <el-radio-group v-model="form.userSex">
            <el-radio :value="1" size="large">男</el-radio>
            <el-radio :value="0" size="large">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="邮箱" prop="userEmail">
          <el-input v-model="form.userEmail" type="email" />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="registerBtn('elForm')"
            class="register"
            style="width: 100%">
            注册
          </el-button>
        </el-form-item>
        <el-form-item>
          <div class="goLogin">
            <span @click="goLogin">已有账号？去登录</span>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script setup>
import { register } from "../../src/utils/https/api";
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
const router = useRouter();

let form = reactive({
  userName: "",
  userPass: "",
  userAge: 1,
  userSex: 1,
  userEmail: "",
});
let elFormRef = ref();

const rules = reactive({
  userName: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  userPass: [{ required: true, message: "请输入密码", trigger: "blur" }],
  userAge: [{ required: true, message: "请输入密码", trigger: "change" }],
  userEmail: [
    {
      type: "email",
      required: true,
      message: "请输入正确的邮箱地址",
      trigger: "blur",
    },
  ],
});

let registerBtn = async () => {
  await elFormRef.value?.validate((valid, fields) => {
    if (!valid) return;
    if (valid) {
      console.log("submit!");
      register(form).then(() => {
        ElMessage.success("恭喜你，注册成功，去登陆吧");
        setTimeout(() => {
          router.back();
        }, 100);
      });
    }
  });
};

let goLogin = () => {
  router.push({
    path: "/",
  });
};
</script>
<style scoped>

.title {
  font-size: 60px;
  text-align: center;
  height: 30%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #7ed6df;
}

.form {
  display: flex;
  justify-content: center;
  align-items: center;
}

.elForm {
  width: 40%;
  background: url('@/image/from.png') no-repeat;
  background-size: 100% 100%;
  padding: 40px 30px 30px;
  border-radius: 20px;
}

.login {
  width: 100%;
  margin-top: 30px;
}

.goRegister {
  width: 100%;
  text-align: right;
  color: #fff;
  text-decoration-line: underline;
  cursor: pointer;
}

.goLogin {
  color: #7ed6df;
  cursor: pointer;
  text-decoration-line: underline;
}
</style>
